import {
  getToken
} from './auth.js'
const BESE_URL = 'https://eaop.cdhongyi.com.cn/'

export const service = (option) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: BESE_URL + option.url,
      data: option.data || {},
      method: option.method || 'GET',
      header: {
        'Content-Type': 'application/json;charset=UTF-8', //'application/x-www-form-urlencoded',
        'Authorization': "Bearer " + getToken(),
      },
      success: res => {
        if (res.data.code === 200 || res.data.code === 0 || res.data.code ===
          1204) {
          resolve(res.data)
        } else if (res.data.code === 500) {
          resolve(res.data)
        } else if (res.data.code === 401) {
          uni.removeStorageSync("ISTEAM")
          uni.showModal({
            content: '请登录后再使用',
            title: '提示',
            success: (res) => {
              if (res.confirm) {
                uni.redirectTo({
                  url: '/pages/login/login'
                })
              }
            }
          })
        }
      }
    })
  })
}
